<?php
/**
 * Created by PhpStorm.
 * User: asus
 * Date: 2018/2/28
 * Time: 15:34
 */

namespace app\index\business;
use app\index\business\BaseBusiness;
use app\index\controller\BaseController;
use app\index\model\UserCollectType;
use app\libs\Predis;
use app\libs\SendSms;
use think\Config;
use think\Request;

class Common extends BaseBusiness
{
    public $redis = null;

    public function __construct()
    {
        $this->redis = new Predis();
    }

    /**
     * curl 请求
     * @param $method
     * @param array $reqData
     * @return array
     */
    public function curl_request_data($method,$reqData = []){
        // 检测路由
        $url = self::getwebsitepath();
        //var_dump($reqData);die();
        if (!$url){
            $this->result['status'] = -1;
            $this->result['msg'] = '路由不对';
            return $this->result;
        }

        // curl1
        $curl_data  = httpRequest($url,$method,$reqData);
        if (!$curl_data){
            $this->result['status'] = -1;
            $this->result['msg'] = 'curl出现了错误';
            return $this->result;
        }

        if ($curl_data['errcode'] != 0){
            $this->result['status'] = -1;
            $this->result['msg'] = 'curl返回数据有问题';
            return $this->result;
        }

        $this->result['status'] = 1;
        $this->result['data'] = $curl_data;
        return $this->result;
    }

    public function _curl_request_data($url,$method,$reqData = []){
        // curl
        $curl_data  = httpRequest($url,$method,$reqData);
        if (!$curl_data){
            return false;
        }

        if ($curl_data['errcode'] != 0){
            return false;
        }
        return $curl_data['data'];
    }

    public static function getwebsitepath(){
        $request = Request::instance();

        $action = $request->action();
        $controller = $request->controller();
        $website = Config::get('cloude.url');
        $apis = Config::get('cloude.'. $controller);
        $api = $apis[$action];
        $url = $website . $api;
        if (preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?\/?/i', $url)) {
            return $url;
        } else {
            return false;
        }
    }

    ###############################################################################################################################################
    /**
     * 用户单品类收藏
     * @param $dispatch
     * @param $data
     * @param $type
     * @return mixed
     */
    public function singleMyCollet($dispatch,$data,$type)
    {
        if (!isset($dispatch['user_no'])){
            array_walk($data,[$this,'_singleMyCollet']);
            return $data;
        }

        $collect_key = Config::get('collect.key'). $dispatch['user_no'];
        $collect_arr = json_decode($this->redis->hget($collect_key,$type));

        if (!$collect_arr){
            $collect_arr = array();
        }

        $curl_key_arr = array_column($data, 'identification');
        $intersect = array_intersect($collect_arr,$curl_key_arr);

        if (empty($intersect)){
            array_walk($data,[$this,'_singleMyCollet']);
            return $data;
        }

        array_walk($data,[$this,'_singleMyCollet'],$intersect);
        return $data;
    }

    public function _singleMyCollet(&$val,$key,$intersect = [])
    {
       if (in_array($val['identification'],$intersect)){
           $val['is_collect'] = 1;
       }else{
           $val['is_collect'] = 0;
       }
    }

    public function clickCollet($dispatch,$data,$type)
    {
        if (!isset($dispatch['user_no'])){
            array_walk($data,[$this,'_singleMyCollet']);
            return $data;
        }

        //选择表名
        $tlb = UserCollectType::$tableName .= $type;
        $model = new UserCollectType();
        $collect_arr = $model->table($tlb)
            ->where('user_no',$dispatch['user_no'])
            ->where('status',1)
            ->column('identification');
        if (!$collect_arr){
            $collect_arr = array();
        }
        //dd($collect_arr);
        $curl_key_arr = array_column($data, 'identification');
        $intersect = array_intersect($collect_arr,$curl_key_arr);
//        var_dump($collect_arr);
//        dd($curl_key_arr);
        if (empty($intersect)){
            array_walk($data,[$this,'_singleMyCollet']);
            return $data;
        }

        array_walk($data,[$this,'_singleMyCollet'],$intersect);
        return $data;
    }
###########################################################################################################################################################

    /**
     * 树状数据返回
     * @param $tree
     * @param int $rootId
     * @param int $level
     * @return array
     */
    public function betree($tree, $rootId = 0,$level=1)
    {
        $return = array();
        foreach ($tree as $leaf) {
            if ($leaf['p_id'] == $rootId) {
                $leaf["level"] = $level;
                foreach ($tree as $subleaf) {
                    if ($subleaf['p_id'] == $leaf['id']) {
                        $leaf['children'] = $this->betree($tree, $leaf['id'], $level + 1);
                        break;
                    }
                }
                $return[] = $leaf;
            }
        }
        return $return;
    }

    public static function get_chain_info($reqData,$url,$method){
        $curl_data  = httpRequest($url,$method,$reqData);
        if (!$curl_data){
            $result['status'] = -1;
            $result['msg'] = 'curl出现了错误';
            $result['data']=[];
            return $result;
        }

        if ($curl_data['errcode'] != 0){
            $result['status'] = -1;
            $result['msg'] = 'curl出现了错误';
            $result['data']=[];
            return $result;
        }

        $result['status'] = 1;
        $result['msg'] = 'success';
        $result['data'] = $curl_data;
        return $result;
    }
}